package com.tony.pmp.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.tony.pmp.common.utils.Constant;
import com.tony.pmp.model.entity.SysMenuEntity;
import com.tony.pmp.model.entity.SysRoleMenuEntity;
import com.tony.pmp.model.mapper.SysMenuMapper;
import com.tony.pmp.model.mapper.SysUserMapper;
import com.tony.pmp.server.service.SysMenuService;
import com.tony.pmp.server.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysMenuServiceImple
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/21/20:24
 * @Description: 菜单
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuEntity> implements SysMenuService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
    * @Description: queryAll 获取菜单列表
    * @Param: []
    * @return: java.util.List<com.tony.pmp.model.entity.SysMenuEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/21 20:52
    */
    @Override
    public List<SysMenuEntity> queryAll() {
        return baseMapper.queryList();
    }

    /**
    * @Description: queryNotButtonList 获取菜单树形层级列表数据
    * @Param: []
    * @return: java.util.List<com.tony.pmp.model.entity.SysMenuEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/22 13:47
    */
    @Override
    public List<SysMenuEntity> queryNotButtonList() {
        return baseMapper.queryNotButtonList();
    }

    /**
    * @Description: queryByParentId 根据父级菜单id查询其下的子菜单列表
    * @Param: [menuId]
    * @return: java.util.List<com.tony.pmp.model.entity.SysMenuEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/23 23:08
    */
    @Override
    public List<SysMenuEntity> queryByParentId(Long menuId) {
        return baseMapper.queryListParentId(menuId);
    }

    /**
    * @Description: delete 根据id删除菜单
    * @Param: [menuId]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/23 23:14
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long menuId) {
        removeById(menuId);
        //删除菜单的角色
        return sysRoleMenuService.remove(new QueryWrapper<SysRoleMenuEntity>().eq("menu_id",menuId));
    }

    /**
    * @Description: getUserMenuList 根据userId获取导航栏菜单
    * @Param: [userId]
    * @return: java.util.List<com.tony.pmp.model.entity.SysMenuEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/8/1 13:22
    */
    @Override
    public List<SysMenuEntity> getUserMenuList(Long userId) {
        List<SysMenuEntity> list = Lists.newArrayList();
        if (userId == Constant.SUPER_ADMIN) {
            list = getAllMenuList(null);
        } else {
            //非超级管理员根据角色获取相应的菜单
            List<Long> menuIdList = sysUserMapper.queryAllMenuId(userId);
            list = getAllMenuList(menuIdList);
        }
        return list;
    }

    /**
    * @Description: getAllMenuList 获取所有菜单列表
    * @Param: [menuIdList]
    * @return: java.util.List<com.tony.pmp.model.entity.SysMenuEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/8/1 13:24
    */
    private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList) {
        List<SysMenuEntity> menuList = queryListByParentId(0L,menuIdList);
        //递归一级菜单下的子菜单
        getMenuTrees(menuList,menuIdList);
        return menuList;
    }

    /**
    * @Description: queryListByParentId 根据parentId获取菜单列表，找出一级菜单列表(类型为目录的菜单)
    * @Param: [parentId, menuIdList]
    * @return: java.util.List<com.tony.pmp.model.entity.SysMenuEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/8/1 15:28
    */
    private List<SysMenuEntity> queryListByParentId(Long parentId,List<Long> menuIdList) {
        List<SysMenuEntity> menuList = baseMapper.queryListParentId(parentId);
        if (menuIdList == null || menuIdList.isEmpty()) {
            return menuList;
        }

        //找出所有一级菜单中存在于“用户分配的菜单列表中”
        List<SysMenuEntity> userMenuList = Lists.newLinkedList();
        for (SysMenuEntity entity : menuList) {
            if (menuIdList.contains(entity.getMenuId())) {
                userMenuList.add(entity);
            }
        }
        return userMenuList;
    }

    /**
    * @Description: getMenuTrees 递归一级菜单下的子菜单(何时开始，何时结束，做什么事)
    * @Param: [menuList, menuIdList]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/8/1 15:35
    */
    private List<SysMenuEntity> getMenuTrees(List<SysMenuEntity> menuList, List<Long> menuIdList) {
        List<SysMenuEntity> subMenuList = Lists.newLinkedList();
        List<SysMenuEntity> tempList;
        for (SysMenuEntity entity : menuList) {
            //当前菜单类型目录(一级菜单)进行遍历(不是目录或子菜单列表为空终止遍历)
            if (entity.getType() == Constant.MenuType.CATALOG.getValue()) {
                tempList = queryListByParentId(entity.getMenuId(),menuIdList);
                entity.setList(getMenuTrees(tempList,menuIdList));
            }
            subMenuList.add(entity);
        }
        return subMenuList;
    }
}
